package com.alu.sg.allstart.allstar;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alu.sg.allstart.R;
import com.alu.sg.allstart.allstar.adapter.MyAdapter1;
import com.alu.sg.allstart.allstar.adapter.MyItemDecoration;
import com.alu.sg.allstart.allstar.bean.NamesData;
import com.alu.sg.allstart.allstar.bean.StartBean;
import com.alu.sg.allstart.base.BaseFragment;
import com.alu.sg.allstart.utils.LetterComparator;
import com.alu.sg.allstart.utils.Pinyin4JUtils;
import com.alu.sg.allstart.weight.WaveSideBarView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sunshine.retrofit.HttpBuilder;
import com.sunshine.retrofit.HttpUtil;
import com.sunshine.retrofit.interfaces.Success;
import com.sunshine.retrofit.utils.FastJsonTools;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Alu on 2017/9/14.
 * version 1.0
 */

public class MyRecycleViewWithMyItemDecorationFragment extends BaseFragment implements MyItemDecoration.ObtainTextCallback {
    private List<NamesData> newNames;
    RecyclerView myRecycle;
    WaveSideBarView waveSideBarView;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_item_decoration;
    }

    @Override
    protected void initView(View view, Bundle saveInstanceState) {

        myRecycle = (RecyclerView) view.findViewById(R.id.rv_myRecycle);
        waveSideBarView = (WaveSideBarView) view.findViewById(R.id.side_view);
        new HttpBuilder("mock/59c07a7fe0dc663341af04cf/okletusgo/getallstart")
                .tag(this)
                .success(json->{
                        StartBean startBean = FastJsonTools.getBean(json, StartBean.class);
                        if (startBean.getStatus() == 0) {
                            loadData(startBean);
                        }
                })
                .get();
    }

    private void loadData(StartBean startBean) {
        List<String> names = new ArrayList<>();
        newNames = new ArrayList<>();
        String[] a = startBean.getData().getName_point().split("丶");
        String[] a1 = startBean.getData().getName_space().split(" ");
        Collections.addAll(names, a);
        Collections.addAll(names, a1);
        for (String name : names) {
            String firstLet = Pinyin4JUtils.getFirstSpell(name.trim());
            NamesData newName = new NamesData(name.trim(), firstLet.toUpperCase());
            newNames.add(newName);
        }
        Collections.sort(newNames, new LetterComparator());
        final MyAdapter1 myAdapter1 = new MyAdapter1(R.layout.item_my_decoration, newNames);
        myRecycle.setLayoutManager(new LinearLayoutManager(getContext()));
        myRecycle.addItemDecoration(new MyItemDecoration(getContext(), this));
        myRecycle.setAdapter(myAdapter1);
        waveSideBarView.setOnTouchLetterChangeListener(letter -> {
            int pos = myAdapter1.getLetterPosition(letter);
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) myRecycle.getLayoutManager();
            linearLayoutManager.scrollToPositionWithOffset(pos, 0);
        });
        myAdapter1.setOnItemClickListener((adapter, view, position) ->
                MyGirlStarDetailActivity.actionStart(getContext(), newNames.get(position).getName()));
    }

    public static MyRecycleViewWithMyItemDecorationFragment newInstance() {
        return new MyRecycleViewWithMyItemDecorationFragment();
    }

    @Override
    public String getFirstText(int position) {
        return newNames.get(position).getPys().substring(0, 1);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        HttpUtil.cancel(this);
    }
}
